package connectfour;

public class GameController {

    public static void main(String args[]) {
        GameController g = new GameController();
    }

    public GameController() {
        // changed the board and gui declaration slightly
        // the gui now requires the board array as part of the constructor so the board has to be made first
        // then once the gui is made, the gui is set in the board using setGUI
    	Config conf = new Config();
    	conf.getConfig();
    	Board board = new Board(conf.getNumBoardRows(), conf.getNumBoardCols(), conf.getConnectn());// switched 6 and 7,
        GUI gui = new GUI(board.getBoard(), conf.getNumBoardRows(), conf.getNumBoardCols(), conf.getConnectn());
        

        // create players depending on game type
        int type = conf.getGameType();
        Player[] player = new Player[2];

        int showDepth = 0; // how many moves ahead to show

        switch (type) {
            case 0: // player vs player
                gui.console.append("PvP\n");
                player[0] = new HumanPlayer(gui);
                player[1] = new HumanPlayer(gui);
                break;

            case 1: // player vs ai
                gui.console.append("PvC\n");
                // get ai level from gui
                int ailevel = conf.getAiLevel();
                player[0] = new HumanPlayer(gui);
                player[1] = new AIPlayer(1, ailevel, gui, showDepth);

                break;

            case 2: // ai vs ai
                gui.console.append("CvC\n");
                // get ai level from gui
                int ailevel1 = conf.getAiLevel();
                player[0] = new AIPlayer(0, ailevel1, gui, showDepth);
                player[1] = new AIPlayer(1, ailevel1, gui, showDepth);
                break;
        }
        
        gui.updateDisplay();


        int winner = -1;
        int nextPlayer = (int) ((Math.random() * 10) % 2);
        if (nextPlayer == 0) {
            gui.console.append("Player 1 (red) goes first\n");
        } else {
            gui.console.append("Player 2 (blue) goes first\n");
        }

        int nextMove;
        boolean validMove;
        while (winner == -1) {
            System.out.println("getting " + nextPlayer + "'s move");
            nextMove = player[nextPlayer].getNextMove(board);
            System.out.println("got " + nextPlayer + "'s move: " + nextMove);
            while (nextMove == -1) {
                System.out.println("getting " + nextPlayer + "'s move");
                nextMove = player[nextPlayer].getNextMove(board);
                System.out.println("got " + nextPlayer + "'s move: " + nextMove);
            }

            if (nextMove != -1) {
                board.doMove(nextPlayer, nextMove);
                //gui.updateDisplay();
                gui.updateDisplay(board);
                        
                nextPlayer = ++nextPlayer % 2;

                winner = board.checkWinner();
                if (winner == -1) {
                    if (nextPlayer == 0) {
                        gui.console.append("Player 1 (red) goes next\n");
                    } else {
                        gui.console.append("Player 2 (blue) goes next\n");
                    }
                }
            //System.out.println("winner" + winner);
            }


        }
        gui.showWinner(winner);
    }
}
